﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.IO;

namespace Share
{

    /// <summary>
    /// JSON 读写类，提供字符串、整型和浮点型的 set 和 get 操作
    /// </summary>
    public class JsonLoader
    {
        public static JsonSerializerSettings settings = new JsonSerializerSettings
        {
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
            Formatting = Formatting.Indented,
            TypeNameHandling = TypeNameHandling.Auto,
            NullValueHandling = NullValueHandling.Ignore
        };

        private JObject m_jsonData = new JObject();

        /// <summary>
        /// 加载 JSON 字符串
        /// </summary>
        /// <param name="strText">JSON 字符串</param>
        /// <returns>是否加载成功</returns>
        public bool LoadText(string strText)
        {
            try
            {
                lock (m_jsonData)
                {
                    m_jsonData = JObject.Parse(strText);
                    OnInit();
                }
                return true;
            }
            catch (Exception ex)
            {
                XKLog.LogError($"LoadText failed: {ex.Message}");
                return false;
            }
        }

        /// <summary>
        /// 加载 JSON 文件
        /// </summary>
        /// <param name="strFileName">文件名</param>
        public void Load(string strFileName)
        {
            try
            {
                lock (m_jsonData)
                {
                    string jsonText = File.ReadAllText(strFileName);
                    m_jsonData = JObject.Parse(jsonText);
                    OnInit();
                }
            }
            catch (Exception ex)
            {
                XKLog.LogError($"Load failed: {ex.Message}");
            }
        }

        /// <summary>
        /// 设置字符串值
        /// </summary>
        /// <param name="strKey">键</param>
        /// <param name="strValue">值</param>
        public void SetValue(string strKey, string strValue)
        {
            lock (m_jsonData)
            {
                m_jsonData[strKey] = strValue;
            }
        }

        /// <summary>
        /// 设置整数值
        /// </summary>
        /// <param name="strKey">键</param>
        /// <param name="intValue">值</param>
        public void SetValue(string strKey, int intValue)
        {
            lock (m_jsonData)
            {
                m_jsonData[strKey] = intValue;
            }
        }

        /// <summary>
        /// 设置浮点数值
        /// </summary>
        /// <param name="strKey">键</param>
        /// <param name="floatValue">值</param>
        public void SetValue(string strKey, float floatValue)
        {
            lock (m_jsonData)
            {
                m_jsonData[strKey] = floatValue;
            }
        }

        /// <summary>
        /// 保存为 JSON 文件
        /// </summary>
        /// <param name="fullName">文件名</param>
        public void Fresh(string fullName)
        {
            try
            {
                lock (m_jsonData)
                {
                    string jsonText = m_jsonData.ToString();
                    File.WriteAllText(fullName, jsonText);
                }
            }
            catch (Exception ex)
            {
                XKLog.LogError($"Fresh failed: {ex.Message}");
            }
        }

        /// <summary>
        /// 获取字符串值
        /// </summary>
        /// <param name="strKey">键</param>
        /// <returns>字符串值或默认值</returns>
        public string GetValueString(string strKey)
        {
            lock (m_jsonData)
            {
                if (m_jsonData.TryGetValue(strKey, out var value) && value.Type == JTokenType.String)
                {
                    return value.ToString();
                }
                XKLog.LogError($"Key not found or invalid type: {strKey}");
                return "";
            }
        }

        /// <summary>
        /// 获取整数值
        /// </summary>
        /// <param name="strKey">键</param>
        /// <returns>整数值或默认值</returns>
        public int GetValueInt(string strKey)
        {
            lock (m_jsonData)
            {
                if (m_jsonData.TryGetValue(strKey, out var value) && value.Type == JTokenType.Integer)
                {
                    return value.ToObject<int>();
                }
                XKLog.LogError($"Key not found or invalid type: {strKey}");
                return -1;
            }
        }

        /// <summary>
        /// 获取浮点数值
        /// </summary>
        /// <param name="strKey">键</param>
        /// <returns>浮点数值或默认值</returns>
        public float GetValueFloat(string strKey)
        {
            lock (m_jsonData)
            {
                if (m_jsonData.TryGetValue(strKey, out var value) && (value.Type == JTokenType.Float || value.Type == JTokenType.Integer))
                {
                    return value.ToObject<float>();
                }
                XKLog.LogError($"Key not found or invalid type: {strKey}");
                return 0;
            }
        }

        /// <summary>
        /// 初始化回调
        /// </summary>
        protected virtual void OnInit() { }
    }
}